package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.ServiceObjSelectAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.ServiceObjectBean;
import com.sx.zongzhi.bean.ServiceObjectTypeBean;
import com.sx.zongzhi.presenter.ServiceObjectPest;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IServiceObjectView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 服务对象选择
 */
public class ServiceObjectActivity extends BaseActivity implements IServiceObjectView{

    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_chileType)
    TextView tv_chileType;
    @BindView(R.id.et_Name)
    EditText et_Name;
    @BindView(R.id.listTitleName)
    TextView listTitleName;
    @BindView(R.id.listTitleContent)
    TextView listTitleContent;
    @BindView(R.id.listView)
    ListView listView;
    private String currentActivity;
    private List<ServiceObjectBean.RowsBean> objectBeanList = new ArrayList<>();
    private ServiceObjSelectAdapter selectAdapter;
    /**服务对象类型*/
    private ServiceObjectTypeBean typeBean;
    /**服务对象子类类型*/
    private ServiceObjectTypeBean.SubTypesBean item;

    private ServiceObjectPest serviceObjectPest;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_service_object);
        setTitleName("服务对象选择");
        ButterKnife.bind(this);
        currentActivity=getIntent().getStringExtra(Global.ACTIVITY_FRAGMENT);
        serviceObjectPest=new ServiceObjectPest(this);
    }

    private void loadData() {
        String name=et_Name.getText().toString().trim();
        if (typeBean == null || item == null) {
            ToastUtils.showToast(this,"请选择服务对象类型");
            return;
        }
        serviceObjectPest.getServiceObjectList(typeBean.getTypeKeyName()
                ,item.getSubTypeKeyName(),name);
    }

    @OnClick({R.id.tv_type,R.id.tv_chileType})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_type:
            case R.id.tv_chileType:
                startActivityForResult(new Intent(this,ServiceObjectTypeActivity.class),1113);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1113://查询返回
                    typeBean = (ServiceObjectTypeBean) intent.getSerializableExtra("ServiceObjectTypeBean");
                    if (typeBean!=null){
                        tv_type.setText(typeBean.getTypeCname());
                        if (typeBean.getDisplayType()==1){//人员
                            listTitleName.setText("姓名");
                            listTitleContent.setText("身份证号");
                            listTitleContent.setVisibility(View.VISIBLE);
                        }else if (typeBean.getDisplayType()==2){//场所
                            listTitleName.setText("场所名称");
                            listTitleContent.setVisibility(View.GONE);
                        }else if (typeBean.getDisplayType()==3){
                            listTitleName.setText("房屋地址");
                            listTitleContent.setText("房主姓名");
                            listTitleContent.setVisibility(View.VISIBLE);
                        }
                    }
                    item = (ServiceObjectTypeBean.SubTypesBean) intent.getSerializableExtra("SubTypesBean");
                    if (item!=null){
                        tv_chileType.setText(item.getSubTypeCname());
                    }
                    loadData();
                    break;
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getServiceObjectList(ServiceObjectBean objectBean) {
        if (objectBean != null && objectBean.getRows()!= null ){
            objectBeanList.clear();
            objectBeanList.addAll(objectBean.getRows());
            if (selectAdapter==null){
                selectAdapter = new ServiceObjSelectAdapter(this,R.layout.item_contact_list,objectBeanList);
                selectAdapter.setType(typeBean.getDisplayType());
                listView.setAdapter(selectAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        MyRxBus.get().post(new MsgEvent<>(currentActivity,1,objectBeanList.get(position)));
                        MyRxBus.get().post(new MsgEvent<>(currentActivity,3,typeBean.getTypeKeyName()));
                        MyRxBus.get().post(new MsgEvent<>(currentActivity,4,item.getSubTypeKeyName()));
                        finish();
                    }
                });

            }else {
                selectAdapter.setType(typeBean.getDisplayType());
                selectAdapter.notifyDataSetChanged();
            }
        }
    }
}
